﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FAP.DataAccess;
using FAP.DataAccess.BLL;
using System.Text.RegularExpressions;
using FAP.Cores.Task;

namespace TestUp
{
    class Program
    {
        static void Main(string[] args)
        {

            nghiepvu nv = new nghiepvu();
            nv.id = 4;
            chitietnghiepvu ctngvu = new chitietnghiepvu();
            ctngvu.nghiepvuid = nv.id;
            ctngvu.websiteid = 4992;
            taikhoan tk = new taikhoan();
            string username = "admin";
            string matkhau = "fapgroup";
            string linkweb = "http://fap.tonghophoanhao.com/";
            string linkthread = "http://fap.tonghophoanhao.com/showthread.php?13-sadasdas-ld-asldasl";
            string title = "asadadas";
            tk = TaiKhoanDAO.GetTaiKhoan(4992, username, matkhau);
            ctngvu = ChiTietNghiepVuDAO.getCTNgiepVuByNghiepVuIDVaWebsiteID(ctngvu);
            string content = "co con chim vang anh";

            UpTask obj_up = new UpTask(ctngvu, content, linkweb,linkthread , title);
            obj_up.m_tk = tk;
            obj_up.onFinished += onFinished;
            obj_up.onError += onError;
            content = obj_up.LastHTML;

            FAP.Cores.Threading.ThreadControl.AddTasks(obj_up);
            //obj_up.RunIt();
            Console.ReadKey();
        }
        private static void onChangeStatus(object sender, object result)
        {
            Console.WriteLine("FUCK");
            //Console.ReadKey();// throw new NotImplementedException();
        }
        private static void onError(object sender, object result)
        {
            Console.WriteLine("FUCK");
            Console.ReadKey();// throw new NotImplementedException();
        }

        private static void onFinished(object sender, object result)
        {
            UpTask pt = (UpTask)sender;       
                if (pt.LastHTML.ToLower().IndexOf("\"_redirectStatus\":\"ok\"".ToLower()) != -1)
                {
                    string linkdaUp = "";
                    linkdaUp = pt.TimKiem(Regex.Unescape(pt.LastHTML), "posts/");
                    //===============================
                    // lay link bai viet truyen vào
                    //linkdaUp = pt.LinkThread + "#post-" + linkdaUp;
                    //===============================
                    linkdaUp = linkdaUp.Remove(linkdaUp.Length - 1);
                    Console.WriteLine(linkdaUp);
                    Console.ReadKey();
                }
                else
                {
                    if (pt.LastHTML != null)
                    {
                        Console.WriteLine(pt.response.ResponseUri);
                    }
                    else
                    {
                        string linkdaUp = "Up thất bại";
                        Console.WriteLine(linkdaUp);
                        Console.ReadKey();
                    }
                }
            }
        }
    }

    

